package cn.shengongying.admin.controller;

import cn.shengongying.admin.dto.ArticleListInDTO;
import cn.shengongying.bean.DataTablePageResult;
import cn.shengongying.bean.ResponseResult;
import cn.shengongying.model.ArticleList;
import cn.shengongying.service.IArticleListService;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * author: RXK
 * date: 2022/2/21 星期一
 * desc:
 */
@Controller
@RequestMapping("/sys/article/list")
public class ArticleListController extends BaseController{


    private final IArticleListService articleListService;

    public ArticleListController(IArticleListService articleListService) {
        this.articleListService = articleListService;
    }


    @RequestMapping("/index")
    public String index() {
        return "/article_list/index";
    }

    @ResponseBody
    @RequestMapping("/list")
    public DataTablePageResult<List<ArticleList>> list(@RequestBody ArticleListInDTO inDTO) {
        List<ArticleList> list = articleListService.list();
        inDTO.setData(list);
        long count = articleListService.count();
        inDTO.setRecordsTotal(count);
        return inDTO;
    }

    @ResponseBody
    @RequestMapping("/delete")
    public ResponseResult<String> delete(@RequestBody ArticleListInDTO inDTO) {
        articleListService.removeById(inDTO.getId());
        return ResponseResult.success();
    }

    @ResponseBody
    @RequestMapping("/detail")
    public ResponseResult<ArticleList> detail(@RequestBody ArticleListInDTO inDTO) {
        ArticleList articleList = articleListService.getById(inDTO.getId());
        return ResponseResult.success(articleList);
    }

    @ResponseBody
    @RequestMapping("/save")
    public ResponseResult<String> save(ArticleListInDTO inDTO, HttpServletRequest request) {
        ArticleList articleList = inDTO.articleList();
        String file;
        try {
            file = multiFile(request);
        } catch (Exception e) {
            logger.error("上传图片出现异常：",e);
            return ResponseResult.fail();
        }
        articleList.setImages(file);
        articleListService.saveOrUpdate(articleList);
        return ResponseResult.success();
    }


}
